/*
  学习目标：修改state中的count : + 1 和 -1
*/

import { legacy_createStore as createStore } from 'redux';

const store = createStore(function (state = { count: 0 }, action) {
  console.warn('action  ----->  ', state.count, action);
  // 2. reducer第二个参数, 接收action对象
  if (action.type === 'add') {
    // 3. 通过返回新值, 使用新值覆盖旧值
    return { count: state.count + 1 };
  }

  if (action.type === 'des') {
    return { count: state.count - 1 };
  }

  return state;
});

// 1. store.dispatch(action对象) 触发action

store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });

console.log('store.getState()  ----->  ', store.getState());
